package org.example.file;

import com.jcraft.jsch.*;

import java.util.Properties;

public class FileTransferWithSFTPExample {
    public static final String REMOTE_HOST = "192.168.51.35";
    public static final String USERNAME = "johnconnor";
    public static final String PASSWORD = "rootroot";
    public static final int REMOTE_PORT = 22;
    public static final int SESSION_TIMEOUT = 10000;
    public static final int CHANNEL_TIMEOUT = 5000;
    public static void main(String[] args) {

        String localFile = "C:\\Users\\JohnConnor\\Documents\\Downloads\\catalina.out";
        String remoteFile = "/home/runtime/test/catalina1.out";
        JSch jsch = new JSch();
        Session jschSession = null;
        try {
            /*jsch.setKnownHosts("/home/johnconnor/.ssh/known_hosts");*/
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            jschSession = jsch.getSession(USERNAME,REMOTE_HOST,REMOTE_PORT);
            jschSession.setPassword(PASSWORD);
            jschSession.setTimeout(SESSION_TIMEOUT);
            jschSession.setConfig(sshConfig);
            Channel sftpChannel = jschSession.openChannel("sftp");
            sftpChannel.connect(CHANNEL_TIMEOUT);
            ChannelSftp channelSftp = (ChannelSftp) sftpChannel;
            channelSftp.put(localFile,remoteFile);
            channelSftp.exit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if (jschSession != null) {
                jschSession.disconnect();
            }
        }

    }
}
